package studiodiva.portfolio.client.model.titles;

import java.util.Iterator;

import static studiodiva.portfolio.client.lib.lang.Lang.map;

import studiodiva.portfolio.client.lib.collections.Map;
import studiodiva.portfolio.client.lib.lang.Base;
import studiodiva.portfolio.client.model.HasApplication;
import studiodiva.portfolio.client.model.titles.titles.Contact;
import studiodiva.portfolio.client.model.titles.titles.Offer;
import studiodiva.portfolio.client.model.titles.titles.Works;

public class TitlesImpl extends Base implements Titles {

  private final Map<TitleType, Title> titles = map();
  private TitleType typeState = TitleType.closed;

  public TitlesImpl(HasApplication hasapplication) {
    initialize(hasapplication);
    titles.put(TitleType.contact, new Contact(this));
    titles.put(TitleType.offer, new Offer(this));
    titles.put(TitleType.done, new Works(this));
  }

  public Title getOpened() {
    return getTitle(typeState);
  }

  public TitleType getState() {
    return typeState;
  }

  public Title getTitle(TitleType type) {
    return titles.value(type);
  }

  public Iterator<Title> iterator() {
    return titles.values().iterator();
  }

  public void open(TitleType value) {
    if (typeState == value) return;
    typeState = value;
    fireEvent(ON_CHANGE, getOpened());
  }
}
